package org.edu.modules.jxzy.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jxzy.entity.JwJiaocai;
import org.edu.modules.jxzy.entity.JwJiaocaiOrder;
import org.edu.modules.jxzy.service.IJwJiaocaiOrderService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.edu.modules.jxzy.service.IJwJiaocaiService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: 教材订单表
 * @Author: jeecg-boot
 * @Date: 2021-04-14
 * @Version: V1.0
 */
@Api(tags = "教材订单表")
@RestController
@RequestMapping("/jxzy/jwJiaocaiOrder")
@Slf4j
public class JwJiaocaiOrderController extends BaseController<JwJiaocaiOrder, IJwJiaocaiOrderService> {
    @Autowired
    private IJwJiaocaiOrderService jwJiaocaiOrderService;

    @Autowired
    private IJwJiaocaiService jwJiaocaiService;

    /**
     * 分页列表查询
     *
     * @param jwJiaocaiOrder
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "教材订单表-分页列表查询")
    @ApiOperation(value = "教材订单表-分页列表查询", notes = "教材订单表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(JwJiaocaiOrder jwJiaocaiOrder,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<JwJiaocaiOrder> queryWrapper = QueryGenerator.initQueryWrapper(jwJiaocaiOrder, req.getParameterMap());
        queryWrapper.orderByDesc("ve_jw_jiaocai_order.addTime");
        Page<JwJiaocaiOrder> page = new Page<JwJiaocaiOrder>(pageNo, pageSize);
//		IPage<JwJiaocaiOrder> pageList = jwJiaocaiOrderService.page(page, queryWrapper);

        //迎合前端条件查询
        if (jwJiaocaiOrder.getJcdm() != null || jwJiaocaiOrder.getJcmc() != null) {
            //根据教材名称或编号找到对应id
            List<JwJiaocai> jiaocais = jwJiaocaiService.listByDMorMC(jwJiaocaiOrder.getJcdm()
                    , jwJiaocaiOrder.getJcmc());

            List<Integer> list = jiaocais.stream().map(jwJiaocai -> jwJiaocai.getId()).collect(Collectors.toList());
            queryWrapper.in("jcid", list);
        }

        IPage<JwJiaocaiOrder> pageList = jwJiaocaiOrderService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param jwJiaocaiOrder
     * @return
     */
    @AutoLog(value = "教材订单表-添加")
    @ApiOperation(value = "教材订单表-添加", notes = "教材订单表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody JwJiaocaiOrder jwJiaocaiOrder) {
        long nowTime = System.currentTimeMillis() / 1000;
        Integer nowTimeInt = new Long(nowTime).intValue();
        jwJiaocaiOrder.setAddtime(nowTimeInt);
        //根据时间生成订单号
        SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMddHHmmss"); // 获取当前时间
        Random random = new Random();
        int ran = random.nextInt(900) + 100; // 随机数100~999
        Date date = new Date();
        String dateran = dmDate.format(date);
        jwJiaocaiOrder.setOrderid(dateran + ran);
        jwJiaocaiOrderService.save(jwJiaocaiOrder);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param jwJiaocaiOrder
     * @return
     */
    @AutoLog(value = "教材订单表-编辑")
    @ApiOperation(value = "教材订单表-编辑", notes = "教材订单表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody JwJiaocaiOrder jwJiaocaiOrder) {
        jwJiaocaiOrderService.updateById(jwJiaocaiOrder);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "教材订单表-通过id删除")
    @ApiOperation(value = "教材订单表-通过id删除", notes = "教材订单表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        jwJiaocaiOrderService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "教材订单表-批量删除")
    @ApiOperation(value = "教材订单表-批量删除", notes = "教材订单表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.jwJiaocaiOrderService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "教材订单表-通过id查询")
    @ApiOperation(value = "教材订单表-通过id查询", notes = "教材订单表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        JwJiaocaiOrder jwJiaocaiOrder = jwJiaocaiOrderService.getById(id);
        if (jwJiaocaiOrder == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(jwJiaocaiOrder);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param jwJiaocaiOrder
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JwJiaocaiOrder jwJiaocaiOrder) {
        return super.exportXls(request, jwJiaocaiOrder, JwJiaocaiOrder.class, "教材订单表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JwJiaocaiOrder.class);
    }


    @AutoLog(value = "教材订单表-入库操作")
    @ApiOperation(value = "教材订单表-入库操作", notes = "教材订单表-入库操作")
    @GetMapping(value = "/orderImport")
    public Result<?> orderImport(@RequestParam(name = "id", required = true) String id) {
        JwJiaocaiOrder jwJiaocaiOrder = jwJiaocaiOrderService.getById(id);
        if (jwJiaocaiOrder == null) {
            return Result.error("未找到对应数据");
        }
        jwJiaocaiOrder.setStatus(1);
        jwJiaocaiOrderService.updateById(jwJiaocaiOrder);
        return Result.OK("入库成功");

    }


    @AutoLog(value = "教材订单表-根据教材id查询对应订单")
    @ApiOperation(value = "教材订单表-根据教材id查询对应订单", notes = "教材订单表-根据教材id查询对应订单")
    @GetMapping(value = "/listByJcid")
    public Result<?> queryPageList(@RequestParam(name = "jcid", required = true) Integer jcid) {
        List<JwJiaocaiOrder> list = jwJiaocaiOrderService.listByJcid(jcid);
        return Result.ok(list);
    }
}
